package cn.wen.yinxiu.works.service.impl;

import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.toolkit.Query;
import cn.wen.yinxiu.works.dao.UsersCollectRelationDao;
import cn.wen.yinxiu.works.entity.UsersCollectRelationEntity;
import cn.wen.yinxiu.works.recommend.UserTopic;
import cn.wen.yinxiu.works.service.UsersCollectRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("usersCollectRelationService")
public class UsersCollectRelationServiceImpl extends ServiceImpl<UsersCollectRelationDao, UsersCollectRelationEntity> implements UsersCollectRelationService {

    @Autowired
    private UsersCollectRelationDao collectDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UsersCollectRelationEntity> page = this.page(
                new Query<UsersCollectRelationEntity>().getPage(params),
                new QueryWrapper<UsersCollectRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Long queryUserCollectWorksCount(Long userId) {
        QueryWrapper<UsersCollectRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("status", StatusEnum.NORMAL_USE.code());
        queryWrapper.eq("is_deleted", DelEnum.NORMAL.code());
        return collectDao.selectCount(queryWrapper);
    }

    @Override
    public List<UserTopic> queryUserCollectTopicByDate(Long userId, String pastDate) {
        return collectDao.queryUserCollectTopicByDate(userId, pastDate, StatusEnum.NORMAL_USE.code(),
                DelEnum.NORMAL.code());
    }

    @Override
    public boolean worksCollectById(Long worksId, Long userId) {
        UsersCollectRelationEntity target = new UsersCollectRelationEntity();
        target.setStatus(StatusEnum.NORMAL_USE.code());
        target.setUserId(userId);
        target.setWorksId(worksId);
        target.setCreateTime(new Date());
        target.setIsDeleted(DelEnum.NORMAL.code());
        target.setUpdateTime(new Date());
        return collectDao.insert(target) > 0;
    }

    @Override
    public boolean worksCancelCollectById(Long worksId, Long userId) {
        QueryWrapper<UsersCollectRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("works_id", worksId);
        queryWrapper.eq("user_id", userId);
        return collectDao.delete(queryWrapper) > 0;
    }

}